package client.comm.baoding.ui

import android.content.Intent
import android.view.View
import client.comm.commlib.comm_ui.draw.SpaceItemDecoration
import client.comm.commlib.widget.EmptyLayout
import client.comm.baoding.R
import client.comm.baoding.ui.vm.TxjlViewModel
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.adapter.KTxjlAdapter
import client.comm.baoding.api.bean.KTxJl
import client.comm.baoding.databinding.ActivityKtxjlBinding
import client.comm.baoding.dialog.MsgTitleDialog
import client.comm.commlib.toast.toast

class KTxjlActivity : BaseMiddleActivity<TxjlViewModel, ActivityKtxjlBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_ktxjl
    }

    val adapter: KTxjlAdapter by lazy {
        KTxjlAdapter(this, mImgLoader).apply {
            cancelKTx = {
                MsgTitleDialog(this@KTxjlActivity).apply {
                    okTxt = "确定"
                    cancleTxt = "关闭"
                    title = "确定取消可提？"
                    ok = {
                        viewModel.kt_cancle(it.order_id)
                    }
                }.show()
            }

            reSend = {
                MsgTitleDialog(this@KTxjlActivity).apply {
                    okTxt = "确定"
                    cancleTxt = "关闭"
                    title = "确定重发短信？"
                    ok = {
                        viewModel.withdraw_order_pay(it.order_id)
                    }
                }.show()
            }
        }
    }

    override fun initView() {
        binding?.apply {
            event = this@KTxjlActivity
            vm = viewModel
            recyclerView.adapter = adapter
            recyclerView.addItemDecoration(SpaceItemDecoration(10))
            emptyLayout.setOnClickListener { viewModel.holdOneKTx() }
            refreshLayout.setOnRefreshListener {
                adapter.hasMoreState.value = true
                viewModel.pageOne.value = 0
                viewModel.holdOneKTx()
            }
            recyclerView.onLoadMore = {
                if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                    viewModel.holdOneKTx()
                }
            }
        }
    }

    override fun observerUI() {
        viewModel.cancleCall.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("取消成功")
                    viewModel.pageOne.value = 0
                    viewModel.holdOneKTx()
                }
            }
        })
        viewModel.withdraw_order_pay.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    it.data?.let {
                        startActivity(Intent(this, Bank3CodeActivity::class.java).apply {
                            putExtra("bank_bind", it)
                            putExtra("status", "3")
                        })
                    }

                }
            }
        })


        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })
        viewModel.callOneKTx.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.withdraw_list as MutableList<KTxJl.Withdraw?>?
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 10) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    if (adapter.itemCount == 0)
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })

    }

    override fun initData() {
        viewModel.holdOneKTx()
    }

    override fun initViewModel(): TxjlViewModel = createViewModel()

}